package com.ymatou.iapi.settlement.testcase;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.optrade.parameter.req.ApplyRefundReq;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.RefundService;
import com.ymatou.iapi.settlement.parameter.OrderSettlementReq;
import com.ymatou.iapi.settlement.service.GenerateInvoicesOnOrderReceivedCall;
import com.ymatou.iapi.settlement.service.PredictCommissionCall;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.PayGateWayCallService;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.database.model.YmtCommissionDetail;
import com.ymttest.database.model.YmtSettlementInvoice;
import com.ymttest.database.sharding.db.sharding.model.RefundBillPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.database.sqlwapper.YmtCommissionDetailWapper;
import com.ymttest.database.sqlwapper.YmtSettlementInvoiceWapper;
import com.ymttest.utils.DataUtil;

/**
 * 支付后生成预估结算佣金接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_PredictCommission {

	private static OrderSettlementReq generateinvoicesonorderreceivedBean;
	private static PredictCommissionCall predictCommissionCall;
	private static GenerateInvoicesOnOrderReceivedCall generateinvoicesonorderreceivedCall;
	
	private static OrderShardingWapper orderShardingWrapper = new OrderShardingWapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("确认收货时生成结算单接口");
	}

	@Before
	public void caseUp() {

		generateinvoicesonorderreceivedBean = new OrderSettlementReq();
		predictCommissionCall = new PredictCommissionCall();
		generateinvoicesonorderreceivedCall = new GenerateInvoicesOnOrderReceivedCall();

	}
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(predictCommissionCall.getOpurl());
		
		orderShardingWrapper.activeDeadCommunicationsLink(orderShardingWrapper);
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void verifyDb() {

		long orderId = generateinvoicesonorderreceivedBean.getOrderId();

		//mq 消息配上后，注释掉该方法
		//	callPredictCommission(orderId);

		try {

			Thread.sleep(20001);

			YmtSettlementInvoiceWapper defaultWapper = new YmtSettlementInvoiceWapper();

			// 佣金
			List<YmtCommissionDetail> ymtCommissionDetailList = new YmtCommissionDetailWapper()
					.selectByOrderId(orderId);

			// 结算佣金
			List<YmtCommissionDetail> ymtSettlemCommissionDetailList = ymtCommissionDetailList
					.stream()
					.filter(commission -> commission.getInvoiceType() == 4)
					.collect(Collectors.toList());

			// 预估结算佣金
			List<YmtCommissionDetail> predictSettlemCommissionDetailList = ymtCommissionDetailList
					.stream()
					.filter(commission -> commission.getInvoiceType() == 14)
					.collect(Collectors.toList());
			
			
			RefundBillPo billPo = orderShardingWrapper.selectRefundBillByOrderId(orderId);
			
			for (int i = 0; i < ymtSettlemCommissionDetailList.size(); i++) {

				Logger.comment("验证确认收货后结算收佣明细表的数据与用户付款后预估的收佣结算数据");

				verifyCommissionDetail(ymtSettlemCommissionDetailList.get(i),
						predictSettlemCommissionDetailList.get(i),
						billPo == null ? true : billPo.getAfterReceiving(),
						billPo);

			}
			
			// 返还佣金
			/*
			 * List<YmtCommissionDetail> ymtBackCommissionDetailList =
			 * ymtCommissionDetailList .stream() .filter(commission ->
			 * commission.getInvoiceType() == 6) .collect(Collectors.toList());
			 * 
			 * // 预估返还佣金,已经没有了预估返还佣金 List<YmtCommissionDetail>
			 * predictBackCommissionDetailList = ymtCommissionDetailList
			 * .stream() .filter(commission -> commission.getInvoiceType() ==
			 * 15) .collect(Collectors.toList());
			 */

			/*for (int i = 0; i < ymtBackCommissionDetailList.size(); i++) {

				Logger.comment("验证确认收货后结算返佣明细表的数据与用户付款后预估的返佣结算数据");

				verifyCommissionDetail(ymtBackCommissionDetailList.get(i),
						predictBackCommissionDetailList.get(i));
			}*/

			Map map = new HashMap();

			map.put("orderId", orderId);
			
			Thread.sleep(60000);
			// 查询交易退款结算表
			List<YmtSettlementInvoice> settlementInvoiceList = defaultWapper
					.selectYmtSettlementInvoice(map);

			Logger.verifyEquals(true, settlementInvoiceList.size() >= 2,
					"验证结算表数据");

			Logger.comment("校验确认收货收佣结算与用户付款后预测的收佣结算数据一致");

			verifyYmtSettlementInvoice(
					settlementInvoiceList
							.stream()
							.filter(settlement -> settlement.getBusinessType() == 4)
							.collect(Collectors.toList()).get(0),
					settlementInvoiceList
							.stream()
							.filter(settlement -> settlement.getBusinessType() == 14)
							.collect(Collectors.toList()).get(0),
					billPo == null ? true : billPo.getAfterReceiving(),
					predictSettlemCommissionDetailList.get(0)
							.getCommissionAmount());

			/*Logger.comment("校验确认收货返佣结算与用户付款后预测的返佣结算数据一致");

			verifyYmtSettlementInvoice(
					settlementInvoiceList
							.stream()
							.filter(settlement -> settlement.getBusinessType() == 6)
							.collect(Collectors.toList()).get(0),
					settlementInvoiceList
							.stream()
							.filter(settlement -> settlement.getBusinessType() == 15)
							.collect(Collectors.toList()).get(0));*/

		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private void verifyCommissionDetail(YmtCommissionDetail expect,
			YmtCommissionDetail actual, boolean afterReceiveing, RefundBillPo billPo) {
		
		Logger.comment("验证CommissionDetail");
		
		if (!afterReceiveing) {
			
			actual.setProdNumOfCommission(actual.getProdNumOfCommission()
					- billPo.getProductCount());
			actual.setCommissionAmount(actual.getCommissionPerProd().multiply(
					new BigDecimal(actual.getProdNumOfCommission().toString())));
			actual.setRealPayAmount(actual.getRealPayAmount().subtract(billPo.getTotalAmount()));
			actual.setRealPayFreight(actual.getRealPayFreight().subtract(billPo.getFreight()));
			actual.setRealPayAmountPerProd(actual.getRealPayAmount().divide(
					new BigDecimal(actual.getProdNumOfCommission().toString())));
			
		}
		
		//DataUtil.verify(expect.getId(), actual.getId(), "验证id");
		/*		DataUtil.verify(expect.getOrderInfoId(), actual.getOrderInfoId(),
						"验证orderInfoId");*/
		DataUtil.verify(expect.getOrderId(), actual.getOrderId(), "验证orderId");
		DataUtil.verify(expect.getProductId(), actual.getProductId(),
				"验证productId");
		DataUtil.verify(expect.getCategoryId(), actual.getCategoryId(),
				"验证categoryId");
		DataUtil.verify(expect.getStrategyId(), actual.getStrategyId(),
				"验证strategyId");
		DataUtil.verify(expect.getStrategyLevel(), actual.getStrategyLevel(),
				"验证strategyLevel");
		DataUtil.verify(expect.getSingleCommissionMax(),
				actual.getSingleCommissionMax(), "验证singleCommissionMax");
		DataUtil.verify(expect.getSingleCommissionMin(),
				actual.getSingleCommissionMin(), "验证singleCommissionMin");
		DataUtil.verify(expect.getSingleCommissionRate(),
				actual.getSingleCommissionRate(), "验证singleCommissionRate");
		DataUtil.verify(expect.getCommissionAmount(),
				actual.getCommissionAmount(), "验证commissionAmount");
		/*		DataUtil.verify(expect.getAddTime(), actual.getAddTime(), "验证addTime");
				DataUtil.verify(expect.getUpdateTime(), actual.getUpdateTime(),
						"验证updateTime");*/
		//		DataUtil.verify(expect.getInvoiceId(), actual.getInvoiceId(),
		//				"验证invoiceId");
		DataUtil.verify(expect.getCommissionPerProd(),
				actual.getCommissionPerProd(), "验证commissionPerProd");
		DataUtil.verify(expect.getRealPayAmountPerProd(),
				actual.getRealPayAmountPerProd(), "验证realPayAmountPerProd");
		DataUtil.verify(expect.getRealPayFreight(), actual.getRealPayFreight(),
				"验证realPayFreight");
		DataUtil.verify(expect.getProdNumOfCommission(),
				actual.getProdNumOfCommission(), "验证prodNumOfCommission");
		/*		DataUtil.verify(expect.getInvoiceType(), actual.getInvoiceType(),
						"验证invoiceType");*/

	}

	private void verifyYmtSettlementInvoice(YmtSettlementInvoice expect,
			YmtSettlementInvoice actual, boolean afterReceiveing, BigDecimal commissionAmount) {
		
		Logger.comment("验证YmtSettlementInvoice");
		
		if (!afterReceiveing) {
		
			actual.setTotalAmount(commissionAmount);
			actual.setCashAmount(commissionAmount);
			actual.setCommissionAmount(commissionAmount);
			
		}

		/*	DataUtil.verify(expect.getInvoiceId(), actual.getInvoiceId(),
					"验证invoiceId");*/
		DataUtil.verify(expect.getBusinessNo(), actual.getBusinessNo(),
				"验证businessNo");
		/*		DataUtil.verify(expect.getBusinessType(), actual.getBusinessType(),
						"验证businessType");*/
		DataUtil.verify(expect.getOrderId(), actual.getOrderId(), "验证orderId");
		DataUtil.verify(expect.getTotalAmount(), actual.getTotalAmount(),
				"验证totalAmount");
		DataUtil.verify(expect.getCashAmount(), actual.getCashAmount(),
				"验证cashAmount");
		DataUtil.verify(expect.getGiftAmount(), actual.getGiftAmount(),
				"验证giftAmount");
		DataUtil.verify(expect.getCouponAmount(), actual.getCouponAmount(),
				"验证couponAmount");
		DataUtil.verify(expect.getSellerId(), actual.getSellerId(),
				"验证sellerId");
		/*		DataUtil.verify(expect.getRemark(), actual.getRemark(), "验证remark");
				DataUtil.verify(expect.getAddTime(), actual.getAddTime(), "验证addTime");*/
		/*		DataUtil.verify(expect.getSettlementTime(), actual.getSettlementTime(),
						"验证settlementTime");*/
		/*		DataUtil.verify(expect.getSettlementStatus(),
						actual.getSettlementStatus(), "验证settlementStatus");*/
		DataUtil.verify(expect.getSettlementUser(), actual.getSettlementUser(),
				"验证settlementUser");
		DataUtil.verify(expect.getCommissionAmount(),
				actual.getCommissionAmount(), "验证commissionAmount");
		DataUtil.verify(expect.getProcessResult(), actual.getProcessResult(),
				"验证processResult");
		DataUtil.verify(expect.getfAccountAmount(), actual.getfAccountAmount(),
				"验证fAccountAmount");
		DataUtil.verify(expect.getfThirdPartyAmount(),
				actual.getfThirdPartyAmount(), "验证fThirdPartyAmount");
		/*		DataUtil.verify(expect.getUpdateTime(), actual.getUpdateTime(),
						"验证updateTime");
				DataUtil.verify(expect.getExpectSettleTime(),
						actual.getExpectSettleTime(), "验证expectSettleTime");*/

	}

	/**
	 * 主动请求,生成预估结算单
	 * @param orderId
	 */
	@SuppressWarnings("unused")
	private void callPredictCommission(long orderId) {

		generateinvoicesonorderreceivedBean.setOrderId(orderId);

		predictCommissionCall.setData(generateinvoicesonorderreceivedBean);

		try {

			predictCommissionCall.callService();

			Logger.info(predictCommissionCall.getReturnData());

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_PredictCommission_001() {
		Logger.start(true, "使用平台券,商家券。确认收货，校验生成下单结算单，佣金结算单 2种结算单");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			// 创建一个拼邮的订单
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();
			// 支付订单
			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);
			// 拼邮国内段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);
			TradingCallService.confirmReceive(userId, orderId);
			Logger.comment("前置数据准备结束");
			
			//调用结算接口
			TradingCallService.generateInvoicesOnOrderReceived(orderId, userId,
					placeOrderResp.getPayableAmount(), sellerId);
			
			generateinvoicesonorderreceivedBean.setOrderId(orderId);
			generateinvoicesonorderreceivedCall
					.setData(generateinvoicesonorderreceivedBean);

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_PredictCommission_003() {
		Logger.start(true, "订单里一个商品全款退款,确认收货后,校验生成订单结算单佣金结算单");
		try {
			Logger.comment("前置数据准备");

			// 创建一个拼邮的订单
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();
			// 支付订单
			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);
			// 拼邮国内段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);
			TradingSyncService.getSyncResult(orderId);
			
			//确认收货
			TradingCallService.confirmReceive(userId, orderId);
			
			// 申请部分退款
			ApplyRefundReq applyRefundReq = RefundService.applyRefund(placeOrderResp.getOrderList().get(0));

			applyRefundReq
					.getSalesRefundInfoList()
					.get(0)
					.setRefundAmount(
							applyRefundReq.getSalesRefundInfoList().get(0)
									.getRefundAmount());

			RefundService.approveRefundBillCall(applyRefundReq);
			
			Logger.comment("前置数据准备结束");
			
			//调用结算接口
			TradingCallService.generateInvoicesOnOrderReceived(orderId, userId,
					placeOrderResp.getPayableAmount(), sellerId);
			
			generateinvoicesonorderreceivedBean.setOrderId(orderId);
			generateinvoicesonorderreceivedCall
					.setData(generateinvoicesonorderreceivedBean);
			/*			generateinvoicesonorderreceivedCall.callService();

			Logger.verifyEquals("ok",
					generateinvoicesonorderreceivedCall.getReturnData(),
					"验证返回报文");*/

			verifyDb();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_PredictCommission_004() {

		Logger.start(true, "订单里一个商品部分退款,确认收货后,校验生成订单结算单佣金结算单");

		try {
			Logger.comment("前置数据准备");

			// 创建一个拼邮的订单
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();
			// 支付订单
			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);
			// 拼邮国内段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);
			TradingSyncService.getSyncResult(orderId);

			// 确认收货
			TradingCallService.confirmReceive(userId, orderId);

			// 申请部分退款
			ApplyRefundReq applyRefundReq = RefundService
					.applyRefund(placeOrderResp.getOrderList().get(0));

			applyRefundReq
					.getSalesRefundInfoList()
					.get(0)
					.setRefundAmount(
							applyRefundReq.getSalesRefundInfoList().get(0)
									.getRefundAmount().subtract(BigDecimal.TEN));

			RefundService.approveRefundBillCall(applyRefundReq);
			Logger.comment("前置数据准备结束");
			
			//调用结算接口
			TradingCallService.generateInvoicesOnOrderReceived(orderId, userId,
					placeOrderResp.getPayableAmount(), sellerId);

			generateinvoicesonorderreceivedBean.setOrderId(orderId);
			generateinvoicesonorderreceivedCall
					.setData(generateinvoicesonorderreceivedBean);
			/*			generateinvoicesonorderreceivedCall.callService();

			Logger.verifyEquals("ok",
					generateinvoicesonorderreceivedCall.getReturnData(),
					"验证返回报文");*/

			verifyDb();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_PredictCommission_005() {
		Logger.start(true, "订单里一个商品部分退款,退款数量小于商品购买数量,确认收货后,校验生成订单结算单佣金结算单");
		try {

			Logger.comment("前置数据准备");

			// 创建一个拼邮的订单
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();
			// 支付订单
			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);
			// 拼邮国内段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);
			TradingSyncService.getSyncResult(orderId);

			// 确认收货
			TradingCallService.confirmReceive(userId, orderId);

			// 申请部分退款
			ApplyRefundReq applyRefundReq = RefundService
					.applyRefund(placeOrderResp.getOrderList().get(0), 1);

			applyRefundReq
					.getSalesRefundInfoList()
					.get(0)
					.setRefundAmount(
							applyRefundReq.getSalesRefundInfoList().get(0)
									.getRefundAmount().subtract(BigDecimal.ONE));

			RefundService.approveRefundBillCall(applyRefundReq);
			Logger.comment("前置数据准备结束");
			
			//调用结算接口
			TradingCallService.generateInvoicesOnOrderReceived(orderId, userId,
					placeOrderResp.getPayableAmount(), sellerId);
			
			generateinvoicesonorderreceivedBean.setOrderId(orderId);
			generateinvoicesonorderreceivedCall
					.setData(generateinvoicesonorderreceivedBean);
			/*			generateinvoicesonorderreceivedCall.callService();

			Logger.verifyEquals("ok",
					generateinvoicesonorderreceivedCall.getReturnData(),
					"验证返回报文");*/

			verifyDb();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_PredictCommission_006() {
		Logger.start(false, "订单号为空");
		try {
			Logger.comment("前置数据准备结束");
			// generateinvoicesonorderreceivedBean.setOrderId(orderId);
			generateinvoicesonorderreceivedCall
					.setData(generateinvoicesonorderreceivedBean);
			generateinvoicesonorderreceivedCall.callService();
			Logger.verifyEquals("ok",
					generateinvoicesonorderreceivedCall.getReturnData(),
					"验证返回报文");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_PredictCommission_011() {
		Logger.start(true, "有运费订单商品，使用平台券,商家券。确认收货，校验生成下单结算单，佣金结算单 2种结算单");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			// 创建一个拼邮的订单
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();
			// 支付订单
			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);
			// 拼邮国内段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);
			TradingSyncService.getSyncResult(orderId);

			// 确认收货
			TradingCallService.confirmReceive(userId, orderId);

			Logger.comment("前置数据准备结束");
			
			//调用结算接口
			TradingCallService.generateInvoicesOnOrderReceived(orderId, userId,
					placeOrderResp.getPayableAmount(), sellerId);

			generateinvoicesonorderreceivedBean.setOrderId(orderId);
			generateinvoicesonorderreceivedCall
					.setData(generateinvoicesonorderreceivedBean);
			/*			generateinvoicesonorderreceivedCall.callService();

						Logger.verifyEquals("ok",
								generateinvoicesonorderreceivedCall.getReturnData(),
								"验证返回报文");*/

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_PredictCommission_012() {
		Logger.start(true, "有运费订单商品,接单后一个商品部分退款,确认收货后,校验生成订单结算单佣金结算单");
		try {
			Logger.comment("前置数据准备");

			// 创建一个拼邮的订单
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();
			// 支付订单
			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			// 申请部分退款
			ApplyRefundReq applyRefundReq = RefundService.applyRefund(
					placeOrderResp.getOrderList().get(0), 1);

			applyRefundReq
					.getSalesRefundInfoList()
					.get(0)
					.setRefundAmount(
							applyRefundReq.getSalesRefundInfoList().get(0)
									.getRefundAmount().subtract(BigDecimal.TEN));

			RefundService.approveRefundBillCall(applyRefundReq);

			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);
			// 拼邮国内段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);
			TradingSyncService.getSyncResult(orderId);

			// 确认收货
			TradingCallService.confirmReceive(userId, orderId);

			Logger.comment("前置数据准备结束");
			
			//调用结算接口
			TradingCallService.generateInvoicesOnOrderReceived(orderId, userId,
					placeOrderResp.getPayableAmount(), sellerId);

			generateinvoicesonorderreceivedBean.setOrderId(orderId);
			generateinvoicesonorderreceivedCall
					.setData(generateinvoicesonorderreceivedBean);
			/*			generateinvoicesonorderreceivedCall.callService();

			Logger.verifyEquals("ok",
					generateinvoicesonorderreceivedCall.getReturnData(),
					"验证返回报文");*/

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_PredictCommission_013() {
		Logger.start(true, "有运费订单商品,接单后一个商品退款加上退部分商品运费,确认收货后,校验生成订单结算单佣金结算单");
		try {
			Logger.comment("前置数据准备");

			// 创建一个拼邮的订单
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();
			long tradingId = placeOrderResp.getTradingId();
			
			//支付
			Long thirdTradingId = TradingCallService.payOrder(userId,
					BigDecimal.ZERO, tradingId, "13");
			PayGateWayCallService.notifyCall("13",
					thirdTradingId.toString(),
					placeOrderResp.getPayableAmount().subtract(BigDecimal.ZERO).toString(),
					(int) userId);
			TradingSyncService.getSyncResult(orderId);
			
			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			
			// 申请部分退款
			ApplyRefundReq applyRefundReq = RefundService
					.applyRefund(placeOrderResp.getOrderList().get(0), 1);

			applyRefundReq
					.getSalesRefundInfoList()
					.get(0)
					.setRefundAmount(
							applyRefundReq.getSalesRefundInfoList().get(0)
									.getRefundAmount().add(BigDecimal.ONE));

			RefundService.approveRefundBillCall(applyRefundReq);
			
			// 支付网关退款回调
			PayGateWayCallService.refundNotifyCall("13",
					thirdTradingId.toString(), (int) userId);
			
			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);
			// 拼邮国内段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);
			TradingSyncService.getSyncResult(orderId);

			// 确认收货
			TradingCallService.confirmReceive(userId, orderId);
			
			Logger.comment("前置数据准备结束");
			
			//调用结算接口
			TradingCallService.generateInvoicesOnOrderReceived(orderId, userId,
					placeOrderResp.getPayableAmount(), sellerId);
			
			generateinvoicesonorderreceivedBean.setOrderId(orderId);
			generateinvoicesonorderreceivedCall
					.setData(generateinvoicesonorderreceivedBean);
			/*			generateinvoicesonorderreceivedCall.callService();

			Logger.verifyEquals("ok",
					generateinvoicesonorderreceivedCall.getReturnData(),
					"验证返回报文");*/

			verifyDb();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_PredictCommission_014() {
		Logger.start(true, "有运费订单商品,确认收货后,校验生成订单结算单佣金结算单");
		try {
			Logger.comment("前置数据准备");

			// 创建一个拼邮的订单
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();
			// 支付订单
			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);
			// 拼邮国内段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);
			TradingSyncService.getSyncResult(orderId);

			// 确认收货
			TradingCallService.confirmReceive(userId, orderId);

			Logger.comment("前置数据准备结束");
			
			//调用结算接口
			TradingCallService.generateInvoicesOnOrderReceived(orderId, userId,
					placeOrderResp.getPayableAmount(), sellerId);
			
			generateinvoicesonorderreceivedBean.setOrderId(orderId);
			generateinvoicesonorderreceivedCall
					.setData(generateinvoicesonorderreceivedBean);
			/*			generateinvoicesonorderreceivedCall.callService();

			Logger.verifyEquals("ok",
					generateinvoicesonorderreceivedCall.getReturnData(),
					"验证返回报文");*/

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
